/**
 * 
 */
package cn.mxz.robots.province;

import java.util.Scanner;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.crawler_single.util.EncoderHandler;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 山东
 * 排名:完成
 * 去年排名:完成
 * 
 * @author cuizhennan<cuizhennan@mouge.wang>
 * @since 2015-06-19
 */
public class ShandongNew extends Exam implements IRank
{
	public static void main(String[] args)
	{
		Student student = new Student("15370687110006", "zhanghan1996", "370684199603111848 ", "15622826152803", "");

		ShandongNew gansu = new ShandongNew(student);
		System.out.println(gansu.getCaptcha());
		System.out.println("请输入验证码：");
		@SuppressWarnings("resource")
		String captcha = new Scanner(System.in).next();
		System.out.println(gansu.login(captcha));
	}

	public ShandongNew(Student student)
	{
		super(student);
	}

	@Override
	public String getCaptcha()
	{
		delCookie();
		Request captchaRequest = new Request("http://wsbm.sdzk.gov.cn/image.jsp", mSha1);
		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://wsbm.sdzk.gov.cn/loginAct.jsp?method=sfzh", mSha1);
		loginRequest.setCharSet("gbk");
		loginRequest.addHeader("Referer", "http://wsbm.sdzk.gov.cn/login.jsp");
		loginRequest.addHeader("Host", "wsbm.sdzk.gov.cn");
		loginRequest.addHeader("Origin", "http://wsbm.sdzk.gov.cn");
		loginRequest.addParams("iptSfzh", mStudent.getIdCard());
		loginRequest.addParams("iptKsmm", EncoderHandler.encodeByMD5(mStudent.getStudentPwd()));
		// loginRequest.addParams("iptKsmm",mStudent.getStudentPwd());
		loginRequest.addParams("iptYzm", captcha);
		loginRequest.addParams("_", "");
		String loginHtml = curlPost(loginRequest);
		System.out.println(loginHtml);
		JSONObject resultObject = new JSONObject();

		if (loginHtml.contains("首页") || loginHtml.contains("select"))
		{
			Request scoreRequest = new Request("http://wsbm.sdzk.gov.cn/cjkView.jsp", mSha1);
			scoreRequest.addHeader("Referer", "http://wsbm.sdzk.gov.cn/login.jsp");
			loginHtml = curlPost(scoreRequest);
			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(loginHtml));
		}
		else if (loginHtml.contains("身份证号填写错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", NOT_EXIST_USER);
		}
		else if (loginHtml.contains("密码不正确"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_PASSWORD);
		}
		else if (loginHtml.contains("验证码不正确"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else
		{
			System.out.println(loginHtml);
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document loginDocument = Jsoup.parse(parseHtml);
		JSONObject result = new JSONObject();
		Elements trsif = loginDocument.select("table").get(2).select("tr");
		result.put("exam_id", mStudent.getStudentId());
		result.put("name", trsif.get(1).select("td").get(1).text());
		result.put("ranking", trsif.get(1).select("td").get(8).text().trim());
		result.put("province", "山东省");
		JSONObject scoreObject = new JSONObject();
		scoreObject.put("语文", trsif.get(1).select("td").get(2).text());
		scoreObject.put("数学", trsif.get(1).select("td").get(3).text());
		scoreObject.put("外语", trsif.get(1).select("td").get(4).text());
		scoreObject.put("综合", trsif.get(1).select("td").get(5).text());
		scoreObject.put("总分", trsif.get(1).select("td").get(6).text());
		scoreObject.put("特征分", trsif.get(1).select("td").get(7).text());

		result.put("score", scoreObject.toString());
		return result.toString();
	}

	@Override
	public void initRank()
	{

	}

	@Override
	public void lastYearRank()
	{
		// 文 http://www.gxeduw.com/news/2014/107178.html |
		// http://www.gxeduw.com/news/2014/107178_2.html
		// 理 http://www.gxeduw.com/news/2014/107179.html |
		// http://www.gxeduw.com/news/2014/107179_2.html
		Request request = new Request("http://www.gxeduw.com/news/2014/107179_2.html", mSha1);
		Document result = Jsoup.parse(curlGet(request));

		Elements trs = result.select("table").get(0).select("tr");
		for (int i = 1; i < trs.size(); i++)
		{
			Integer scores = Integer.parseInt(trs.get(i).select("td").get(0).text().replaceAll(" ", ""));
			Integer counters = Integer.parseInt(trs.get(i).select("td").get(1).text());
			Integer rankings = Integer.parseInt(trs.get(i).select("td").get(2).text());

			System.out.println(scores + ":" + counters + ":" + rankings);

			GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
			GaoKaoProvince province = provinceService.get(22);
			GaoKaoRankService rankService = new GaoKaoRankService();
			GaoKaoRank rank = new GaoKaoRank();
			rank.setScore(scores);
			rank.setCount(counters);
			rank.setType(LI_KE);
			rank.setRanking(rankings);
			rank.setGaoKaoProvince(province);
			rankService.save(rank);
		}
	}

}
